package com.hacker.concurrent.com.hacker.thread.base;

/**
 * @Author:这个程序员有纹身
 * @Date: Created in 21:04 2017\12\19 0019
 * 隋唐演义大戏舞台
 */
public class Stage extends Thread{

    public void run(){

        ArmyRunnable armyTaskOfSuiDynasty = new ArmyRunnable();
        ArmyRunnable armyTaskOfRevolt = new ArmyRunnable();

        //使用Runnable接口创建线程
        Thread armyOfSuiDynasty = new Thread(armyTaskOfSuiDynasty,"隋军");
        Thread armyOfRevolt = new Thread(armyTaskOfRevolt,"农民义军");

        //启动线程，让军队开始作战
        armyOfSuiDynasty.start();
        armyOfRevolt.start();

        //舞台线程休眠，大家专心观看军队厮杀
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("正当双方激战正酣，半路杀出了个程咬金");

        Thread mrCheng = new KeyPersonThread();
        mrCheng.setName("程咬金");

        System.out.println("程咬金的理想是结束战争，使百姓安居乐业！");

        //停止军队作战（停止线程）
        armyTaskOfSuiDynasty.keepRunning = false;
        armyTaskOfRevolt.keepRunning = false;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*
         * 历史大戏留给关键人物
         */
        mrCheng.start();

        try {
            mrCheng.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("战争结束，人民安居乐业，程咬金实现了梦想，为人民做出了贡献！");
        

//        armyTaskOfSuiDynasty.keepRunning = false;
//        armyTaskOfRevolt.keepRunning = false;
//
//        try {
//            armyOfRevolt.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public static void main(String[] args){
        new Stage().start();
    }
}
